/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package warehouse;

import entity.Warehouse;
import facade.WarehouseFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import warehouse.sorts.SortByAddress;
import warehouse.sorts.SortByDuration;
import warehouse.sorts.SortByName;
import warehouse.sorts.SortByShipmentRate;

/**
 *
 * @author Tkach Andrii
 */
@ServletSecurity(
        @HttpConstraint(rolesAllowed = {"Warehouse Employee", "Administrator"}))
public class ViewWarehouse extends HttpServlet {

    @EJB
    private WarehouseFacadeLocal warehouseFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");


        Warehouse warehouse;

        int pageNumber;
        String pageNum = request.getParameter("pageNumber");
        if (pageNum != null) {
            pageNumber = Integer.parseInt(pageNum);
        } else {
            pageNumber = 1;
        }
        int maxResults = 5;
        int some = warehouseFacade.count();
        int temp1 = (warehouseFacade.count() / maxResults);
        int numberOfPages;
        if ((some % maxResults) == 0) {
           numberOfPages = Math.round((warehouseFacade.count() / maxResults))+1;
        } else {
           numberOfPages = Math.round((warehouseFacade.count() / maxResults));
        }
        int firstResult = maxResults * (pageNumber - 1);

        String name = request.getParameter("Find");

        if (name == null) {
            name = "";
        }
        ArrayList<Warehouse> warehouseList = new ArrayList<Warehouse>();

        if (name != "") {
            for (Warehouse ws : warehouseFacade.findByName(name, firstResult, maxResults)) {
                warehouseList.add(ws);
            }
        } else {
            for (Warehouse ws : warehouseFacade.findRange(firstResult, maxResults)) {
                warehouseList.add(ws);
            }
        }


        String selectedValue = "";

        if (request.getParameter("sort") != null) {
            if (request.getParameter("sort").equals("Name")) {
                Collections.sort(warehouseList, new SortByName());
                selectedValue = "Name";

            }
            if (request.getParameter("sort").equals("Address")) {
                Collections.sort(warehouseList, new SortByAddress());
                selectedValue = "Address";
            }
            if (request.getParameter("sort").equals("ShipmentRate")) {
                Collections.sort(warehouseList, new SortByShipmentRate());
                selectedValue = "ShipmentRate";
            }
            if (request.getParameter("sort").equals("Duration")) {
                Collections.sort(warehouseList, new SortByDuration());
                selectedValue = "Duration";
            }
        } else {
            //default sort
            Collections.sort(warehouseList, new SortByName());
            selectedValue = "Name";
        }

        request.setAttribute(
                "selectedValue", selectedValue);
        request.setAttribute(
                "WarehouseList", warehouseList);
        request.setAttribute("name", name);
        request.setAttribute("pageNumber", pageNumber);
        request.setAttribute("numberOfPages", numberOfPages);


        RequestDispatcher dispatcher;
        dispatcher = getServletContext().getRequestDispatcher("/warehouse.jsp");

        dispatcher.forward(request, response);
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
